package com.ian.dao;

import com.ian.entity.Message;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;

/**
 * A repository for accessing {@link Message}s.
 *
 * @author Witt
 * @version 1.0.0
 * @date 2022/5/11
 */
@Repository
public interface MessageRepositoryBackup extends PagingAndSortingRepository<Message, Long> {

    /**
     * Spring Security Expressions within @Query
     * This checks to see if the Authentication.getPrincipal().getId() is equal to the recipient of the Message.
     * Note that this example assumes you have customized the principal to be an Object that has an id property.
     * By exposing the SecurityEvaluationContextExtension bean, all of the Common Security Expressions are available within the Query.
     *
     * @param pageable
     * @return
     */
    @Query("select m from Message m where m.to.id = ?#{ principal?.id }")
    Page<Message> findInbox(Pageable pageable);
}
